﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.Xml;
using System.ComponentModel;

using ShootSearch.Util;
using ShootSearch.Util.Serializers;

namespace ShootSearch.Core
{
    [Serializable]
    [TypeConverter(typeof(ExpandableConverter<AssemblyWithConfig>))]
    public class AssemblyWithConfig : IXmlSerializable
    {
        protected SpiderManConfig config;
        protected AssemblyConfig type;

        public AssemblyConfig Type
        {
            get { return type; }
            set
            {
                this.type = value;
                if (type != null)
                {
                    IConfigable obj = type.GetObject() as IConfigable;
                    if (obj != null)
                    {
                        Config = obj.Config;
                    }
                }
            }
        }

        public SpiderManConfig Config
        {
            get { return config; }
            set
            {
                config = value;
                //if (value != null)
                //{
                //    configType = new AssemblyConfig(value.GetType());
                //}
            }
        }

        public AssemblyWithConfig()
        {
            Type = new AssemblyConfig();
            //ConfigType = new AssemblyConfig();
        }

        public AssemblyWithConfig(IConfigable obj)
        {
            if (obj != null)
            {
                Type = new AssemblyConfig(obj.GetType());
                if (obj.Config != null)
                {
                    Config = obj.Config;
                }
            }
        }

        public AssemblyWithConfig(Type type)
        {
            if (type != null)
            {
                Type = new AssemblyConfig(type);
                IConfigable obj = type.Assembly.CreateInstance(type.FullName) as IConfigable;
                if (obj != null)
                {
                    Config = obj.Config;
                }
            }
        }


        public AssemblyWithConfig(Type type, SpiderManConfig config)
        {
            if (type != null)
            {
                Type = new AssemblyConfig(type);
                Config = config;
            }
        }


        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return new System.Xml.Schema.XmlSchema();
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {

            Type = new AssemblyConfig();
            reader.ReadToFollowing("Type");
            reader.MoveToAttribute("TypeName");
            Type.TypeName = reader.Value;
            reader.MoveToAttribute("Assembly");
            Type.Assembly = reader.Value;

            
            IConfigable obj = Type.GetObject() as IConfigable;
            if (obj != null && obj.Config!=null)
            {
                Type type = obj.Config.GetType();
                if (type != null && reader.ReadToFollowing(type.Name))
                {
                    string debug = reader.ReadOuterXml();
                    Config = XmlFormatter.FromXml(debug, type) as SpiderManConfig;

                }
            }
        }

        public void WriteXml(System.Xml.XmlWriter writer)
        {


            writer.WriteStartElement("Type");
            if (Type != null)
            {
                writer.WriteAttributeString("TypeName", Type.TypeName);
                writer.WriteAttributeString("Assembly", Type.Assembly);
            } 
            writer.WriteEndElement();

            //writer.WriteStartElement("ConfigType");
            //if (ConfigType != null)
            //{
            //    writer.WriteAttributeString("TypeName", ConfigType.TypeName);
            //    writer.WriteAttributeString("Assembly", ConfigType.Assembly);
            //}
            //writer.WriteEndElement();

            XmlFormatter.ToXml(writer, Config);
        }

        #endregion

        public object GetObject()
        {
            if (Type != null) return Type.GetObject();
            else return null;
        }


        public IConfigable GetObjectWithConfig()
        {
            if (Type != null) 
            {
                IConfigable obj = Type.GetObject() as IConfigable;
                if (obj != null)
                {
                    obj.Config = this.Config;
                    return obj;
                }
            }
            return null;
        }

        public override string ToString()
        {
            if (Type != null)
            {
                return Type.ToString();
            }
            else return base.ToString();
        }
    }
}
